package com.common.es.config;

import com.common.es.instance.EsInstance;
import org.apache.http.HttpHost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 牧鱼
 * @Classname EsConfig
 * @Description TODO
 * @Date 2021/5/10
 */
@Component
@ConfigurationProperties(value = "es")
public class EsConfig {
    private static final Logger log = LoggerFactory.getLogger(EsConfig.class);
    /**
     * Es集群 配置节点
     */
    private List<EsClusterNode> esClusterNodes = new ArrayList<>();

    /**
     * 开启密码认证
     */
    private boolean isAuth = false;

    private String userName;

    private String password;

    public List<EsClusterNode> getEsClusterNodes() {
        return esClusterNodes;
    }

    public void setEsClusterNodes(List<EsClusterNode> esClusterNodes) {
        this.esClusterNodes = esClusterNodes;
    }


    public boolean isAuth() {
        return isAuth;
    }

    public void setIsAuth(boolean auth) {
        isAuth = auth;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public HttpHost[] initHttpHost(){
        return new ArrayList<HttpHost>(){{
            esClusterNodes.forEach( (esClusterNode) -> {
                add(new HttpHost(esClusterNode.getIp(),esClusterNode.getPort()));
                log.info("初始化Es成功：{IP："+esClusterNode.getIp()+"  Port："+esClusterNode.getPort()+"}");
            });
        }}.toArray(new HttpHost[esClusterNodes.size()]);
    }

}
